#include <iostream>
#include <thread>
#include <vector>
#include <functional>

std::mutex g_mutex; 

class Task {
public:
    Task(int id) : taskId(id) {}

    // 执行任务
    void execute() {
        std::lock_guard<std::mutex> lock(g_mutex);       
        std::cout << "Task " << taskId << " is executing in thread: " << std::this_thread::get_id() << std::endl;
    }

private:
    int taskId;  // 每个任务的唯一ID
};

void runTask(Task task) {
    task.execute();
}

int main() {
    std::vector<std::thread> threads;
    
    // 创建多个任务，并行执行
    for (int i = 0; i < 5; ++i) {
        Task task(i); // 每个任务使用独立的数据
        threads.push_back(std::thread(runTask, task)); // 任务在不同线程中执行
    }

    // 等待所有线程执行完成
    for (auto& t : threads) {
        t.join();
    }

    return 0;
}

